package com.zeroone.star.sample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeroone.star.project.dto.PageDTO;
import com.zeroone.star.project.dto.sample.SampleDTO;
import com.zeroone.star.project.query.sample.SampleQuery;
import com.zeroone.star.sample.entity.User;
import com.zeroone.star.sample.mapper.UserMapper;
import com.zeroone.star.sample.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Mapper(componentModel = "spring")
interface MsSampleMapper {
    SampleDTO user2sampledto(User user);
}

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 张大帅
 * @since 2024-10-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    MsSampleMapper msSampleMapper;

    /**
     * 分页查询所有
     *
     * @param condition 查询条件
     * @return 查询结果
     */
    @Override
    public PageDTO listAll(SampleQuery condition) {
        //构建分页对象
        Page<User> page = new Page<>(condition.getPageIndex(), condition.getPageSize());
        //构建查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(condition.getName() != null, User::getName, condition.getName());
        //执行查询
        Page<User> result = baseMapper.selectPage(page, wrapper);
        //返回结果
        return PageDTO.create(result, msSampleMapper::user2sampledto);
    }

    /**
     * 根据id查询
     * @param id 用户id
     * @return 查询结果
     */
    @Override
    public SampleDTO getById(int id) {
        User user = baseMapper.selectById(id);
        if (user != null) {
            return msSampleMapper.user2sampledto(user);
        }
        return null;
    }
}
